package com.shiqla.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.net.InetAddress;

/**
 * Desc ${DESC}
 * Auth Shinelon
 * Date 2018/4/24
 */
public class DiscardServerhandler extends SimpleChannelInboundHandler<String> {

    /**
     * 用于处理接收到的 handler
     * @param channelHandlerContext
     * @param s
     * @throws Exception
     */
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {

        //打印消息
        System.out.println(channelHandlerContext.channel().remoteAddress() + " say msg: " + s);

        //给客户端响应
        channelHandlerContext.writeAndFlush("Received your message \n");
    }

    /**
     * 当有客户端接入
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        System.out.println("RamoterAddress : "  + ctx.channel().remoteAddress() + "  active !");
        ctx.writeAndFlush("Weclome to " + InetAddress.getLocalHost().getHostName() + "  service !");
        super.channelActive(ctx);
    }
}
